﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NLBS.Framework;
using NLBS.Data.Business;
using NLBS.Security;
using NLBS.Entity;
using NLBS.Views;
using NLBS.Text;

namespace NLBS.Web.UI
{
   public class GuestBookCommand:ICommand,IAction<GuestBook>
    {
        #region Presenter

       private GuestBookPresenter _presenter;

       public GuestBookPresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new GuestBookPresenter();
                this._presenter.Action = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            this.Presenter.OnActionLoaded();
        }
        #endregion
        private Action act;
        private CallBackBase callback;
        private HttpRequest request;
       public GuestBookCommand(Action act, CallBackBase callBack)
       {
           this.act = act;
           this.callback = callBack;
       }
        
        #region ICommand Members

       public void Excute()
       {
           request = callback.Page.Request;
           PresenterInit();
           switch (act)
           {
               case Action.Insert:
                   InsertGbook();
                   break;
               case Action.Delete:

                   DeleteBook();
                   break;
               case Action.DeleteMulit:
                   DeleteMulitBook();
                   break;

           }
       }

        private void DeleteBook()
        {
            this.keyID = int.Parse(request.Form["id"]);
            this.DataDelete(null, null);
        }

        private void DeleteMulitBook()
        {
            string[] Ids = request.Form["Ids"].Split(new char[] { ',' });
            foreach (string item in Ids)
            {
                this.keyID = int.Parse(item);
                this.DataDelete(null, null);
            }
        }

        private void InsertGbook()
        {
            StringSecurity.CheckSecurityCode(request.Form["scode"]);
            string name = request.Form["name"];
            string message = request.Form["message"];
            GuestBook guestBook = new GuestBook();
            
            guestBook.UserName = name;
            guestBook.UserID = BlogInfo.Instance().CurrentUser.UserID;
            guestBook.Content = HtmlHelper.HtmlSafe(message);
            guestBook.EditMark = string.Empty;
            guestBook.UbbFlags = "111111";
            guestBook.PostTime = DateTime.Now;
            guestBook.ReplyUserName = string.Empty;
            guestBook.Reply = string.Empty;
            guestBook.ReplyTime = DateTime.Now;
            guestBook.Hidden = false;
            guestBook.IP = request.UserHostAddress;
            this.Entity = guestBook;
            this.DataInsert(null, null);
        }

        #endregion

        #region IAction<GuestBook> Members

        private GuestBook _entity;
        public GuestBook Entity
        {
            get
            {
                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private int _keyID;
        public int keyID
        {
            get
            {
                return _keyID;
            }
            set
            {
                _keyID = value;
            }
        }

        public event EventHandler DataInsert;

        public event EventHandler DataUpdate;

        public event EventHandler DataDelete;

        public event EventHandler DataSelectSingle;

        #endregion
    }
}
